angular.module('ted.controller', ['ted.service','ui.bootstrap'])
.controller('MenuController', ['$scope', '$route','$location', 'Ted', 
function($scope,$route,$location,Ted) {

    var url = $location.absUrl();
    var menus = Ted.getMenus();
    for(var i=0,l=menus.length;i<l;i++){
        menus[i].actived = false;
        if(url.indexOf(menus[i].url) >= 0 ){
            menus[i].actived = true;
        }
    }
    $scope.menus = menus;
    $scope.clickmenu = function(idx){
        for(var i=0,l=menus.length;i<l;i++){
            menus[i].actived = false;
        }
        menus[idx].actived = true;
        $route.reload();
    };
}])
.controller('TopicController', [
    '$scope','$routeParams','$modal','$timeout', 'Ted', 
function($scope,$routeParams,$modal,$timeout,Ted) {



    var id = $routeParams.id;
    Ted.getTopic(id, function(data){
        $scope.topic = data;     

        if(!!data.mkdoc){
            $scope.topic.mkdocPath = '/mkdocs/' + data.mkdoc;
        }
        
        $scope.rateRo = false;

        if(!$scope.topic.valid){
          $scope.alerts = [{ 
            type: 'danger', 
            msg: '此活动已经结束'}
            ];
        }
    });


    $scope.addScore = function (){
        if($scope.topic.ro){
            // alert('不要重复打分哟!');
        }else{
            $scope.topic.ro = true;
            Ted.addScore($scope.topic.tid,$scope.topic.rate,function(){});
        }
        
    };



    //enroll
    $scope.enroll = function (topic) {

    var modalInstance = $modal.open({
      templateUrl: 'tpl/enroll.tpl.html',
      controller: 'EnrollController',
      // size: 'sm',
      resolve: {
        topic: function () {
          return topic;
        }
      }
    });

    modalInstance.result.then(
    function (data) {
        $scope.alerts = [
            { 
                type: 'success', 
                msg: '恭喜您成功报名'+data.topic.title+'!!!' }
        ];

        $scope.closeAlert = function(index) {
            $scope.alerts.splice(0, 1);
        };

        $timeout(function(){
             $scope.alerts.splice(0, 1);
        }, 3000);


    }, function (data) {
      console.log("error:"+ data);
    });
    };    
}])
.controller('EnrollController', [
    '$scope','$modalInstance','$http','topic',
function($scope,$modalInstance,$http,topic) {
  $scope.topic = topic;
  $scope.confirm = function () {
    var data = {
        topic:$scope.topic,
        enroll:$scope.enroll
    };
    $http.post('/_api/enroll',data).
        success(function(r, status, headers, config) {
            $modalInstance.close(data);
        }).
        error(function(data, status, headers, config) {
        });    

  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };

}])
.controller('TopicsController', ['$scope', 'Ted', '$routeParams',
function($scope,Ted, $routeParams) {
    Ted.getTopics(function(data){
        $scope.topics = data;        
    });

}])
.controller('FooterController', ['$scope','$location','$anchorScroll',
function($scope,$location,$anchorScroll) {

    $scope.gototop = function(){
        $location.hash('pagetop');
        $anchorScroll();
    };

}])
.controller(
    'TagController', 
    ['$scope','$routeParams','Ted',
function($scope,$routeParams,Ted) {
    var tag = $routeParams.tag;
    $scope.info = '标记为 "' + tag + '" 的主题';
    Ted.getTopicByTag(tag,function(data){
        $scope.topics = data;
    });
}]);
